<?php
namespace Mentees\Controller;

class AppointmentController extends MainController {

    //接受到的预约
    public function index(){
        $obj = M('Consult');
        $map['toID']=$this->userID;
        $count = $obj->where($map)->count();
        import("Common.ORG.Page");
        $page = new \Page($count, 10);
        $show = $page->show();
        $list = $obj->where($map)->order('id desc')->limit($page->firstRow . ',' . $page->listRows)->select();
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->display();
    } 
    //发出到的预约
    public function sendbox(){
        $obj = M('Consult');
        $map['fromID']=$this->userID;
        $count = $obj->where($map)->count();
        import("Common.ORG.Page");
        $page = new \Page($count, 10);
        $show = $page->show();
        $list = $obj->where($map)->order('id desc')->limit($page->firstRow . ',' . $page->listRows)->select();
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->display();
    } 


    //添加时间预约
    public function add(){
        $toID = I('get.toid');
        $toUser = M('Mentors')->where('userid='.$toID)->find();
        $fromUser = M('Mentees')->where('userid='.$this->userID)->find();
        $this->assign('fromUser',$fromUser);
        $this->assign('toUser',$toUser);
        $state = M('States')->select();
        $this->assign('state',$state);
        $this->display();
    } 

    public function save(){
        $obj = D('Consult');
        if ($data = $obj->create()) {
            if ($list = $obj->add($data)) {                
                $state = 'SUCCESS';
                $message = L('_action_success');

                //发送系统通知
                $content="You have an appointment from ".$data['fromName'].".";
                $title="You have a new appointment.";
                $this->sendSysMsg($data['toID'],$title,$content);
            } else {
                $state = L('_action_fail');
            }            
        } else {
            $state = $obj->getError();
        }
        echo $this->echo_json_str($state,$message); 
    } 

    public function update(){
        $obj = D("Consult");
        $userid=$this->userID;
        if ($data = $obj->create()) {
            $consult=M("Consult")->where("id=".$data['id']."")->find();
            if($consult['status']!=1 and $consult['fromID']==$userid){
                $data['status']=0;
            }
            if ($list = $obj->save($data)) {  
                echo $this->echo_json_str("SUCCESS",L('_action_success'));exit;
            } else {
                echo $this->echo_json_str(L('_action_fail'));exit;
            }            
        } else {
            echo $this->echo_json_str($obj->getError());exit;
        }
    }

     //更新状态
    public function updatestate(){
        $status = I('post.status');
        $remark = I('post.remark');
        $id = I('post.id');        

        if ($status==-1 && $remark=='') {
            echo $this->echo_json_str('Please enter the reason for the refusal.');exit;
        }

        $data['status'] = $status;
        $data['remark'] = $remark;
        $map['id'] = $id;
        $map['toID']=$this->userID;
        $obj = M("Consult")->where($map)->save($data);

        $consult=M("Consult")->where("id=".$id."")->find();
        if($data['status']==1){
            $content=$consult["toName"]." has agreed to your appointment.time:".date("F,d,Y",$consult['time']);
            $title=$consult["toName"]." has agreed to your appointment.";
         }else{
            $content=$consult["toName"]." has rejected your appointment.time:".date("F,d,Y",$consult['time']).";reason:".$data['remark'];
            $title=$consult["toName"]." has rejected to your appointment.";
         }
        $this->sendSysMsg($consult['fromID'],$title,$content);     

        echo $this->echo_json_str("SUCCESS",L('_action_success'),$_SERVER['HTTP_REFERER']);exit;
    }

    public function edit(){
        $id = (int)I('get.id');
        $userid=$this->userID;
        $obj = M('Consult');
        $map['id']=$id;
        $list = $obj->where($map)->find();
        if (!$list) {
            $this->error(L('_public_noinfo'));
        } else {
            if($list['toID']==$userid && $list['status']==0){
               $obj->where($map)->setField("status",2); 

            }
            $this->assign('userid',$userid);
            $this->assign('list',$list);

            $state = M('States')->select();
        $this->assign('state',$state);
            $this->display();
        }
    }

    //删除预约
    public function del(){
        $id = I('get.id');
        $map['id'] = $id;  
        $obj = M('Consult');
        $obj->where($map)->delete();
        $this->success(L("_action_success"));
    }

    //发送预约通知
    public function sendAppMsg($id){
        if($id){
            $consult=M("Consult")->where("id=".$id."")->find();
            $title="There is a appointment from ".$consult["fromName"].".";
            $content="There is a appointment from ".$consult["fromName"].".".date("F,d,Y",$consult['time']);
            $this->sendSysMsg($consult['toID'],$title,$content);  
        }
    }

    //查看预约信息
    public function view(){
        $id = (int)I('get.id');
        $userid=$this->userID;
        $obj = M('Consult');
        $map['id']=$id;
        $map['toID']=$this->userID;
        $list = $obj->where($map)->find();
        if (!$list) {
            $this->error(L('_public_noinfo'));
        } else {
            if($list['status']==0){
               $obj->where($map)->setField("status",2); 
            }
            $this->assign('userid',$userid);
            $this->assign('list',$list);
            $this->display();
        }
    }
}